package com.example.capturetheflag.joinGame;

import java.util.ArrayList;
import java.util.List;
import com.example.capturetheflag.R;
import com.example.capturetheflag.connection.HttpPostCreatePlayerRequest;
import com.example.capturetheflag.helper.Singleton;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.view.Menu;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;

public class JoinMenuActivity extends Activity implements LocationListener {

	private Spinner GameName, Team;
	private EditText playerName;
	public Dialog dialog;
	private Location location;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_join_game);
        
        Team = (Spinner) findViewById(R.id.spinnerTeam);
		List<String> list = new ArrayList<String>();
		list.add("The Blues");
		list.add("The Reds");
		ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item,list);
		dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		Team.setAdapter(dataAdapter);

		
		String[] gamesTab = getIntent().getExtras().getStringArray("games");
		GameName = (Spinner) findViewById(R.id.spinnerGame);
		List<String> list1 = new ArrayList<String>();
		for(int i=0;i<gamesTab.length;i++)
			list1.add(gamesTab[i]);
		ArrayAdapter<String> dataAdapter1 = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item,list1);
		dataAdapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		GameName.setAdapter(dataAdapter1);
		
		playerName = (EditText)findViewById(R.id.playerName);
		
		LocationManager lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
    	lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000L, 500.0f, this);
    		
    	LocationManager locManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);         

    	locManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,1000L,500.0f, this);
    	location = locManager.getLastKnownLocation(LocationManager.GPS_PROVIDER); 
    	
    	dialog = new Dialog(JoinMenuActivity.this);
    	
    	 Button join = (Button) findViewById(R.id.joinButton);
    	    join.setOnClickListener(new View.OnClickListener() 
    	    {
    	        public void onClick(View view) 
    	        {

    	        	int latitude =  (int)(location.getLatitude()*1e6);
    	        	int longitude = (int)(location.getLongitude()*1e6);
    	        	
    	        	String team;
    	        	if(Team.getSelectedItem().toString().equals("The Blues")){
    	        		Singleton.getInstance().team = true;
    	        		team = "true";
    	        	}else{
    	        		Singleton.getInstance().team = false;
    	        		team = "false";
    	        	}
    	        	Singleton.getInstance().flag = false;
    	        	Singleton.getInstance().gameName = GameName.getSelectedItem().toString();
    	        	
    	        	HttpPostCreatePlayerRequest httpPostRequest = new HttpPostCreatePlayerRequest(getApplicationContext(),JoinMenuActivity.this);
    	        	httpPostRequest.execute("http://proinz.studentlive.pl/GeoService.svc/CreatePlayer" , "CreatePlayer"
    	        			, Singleton.getInstance().gameName, playerName.getText().toString(), team,
    	        			"false", Integer.toString(latitude), Integer.toString(longitude));
    	        	
    	        	dialogFun();
    	        }
    	    });   	    
    }
    
    public void dialogFun(){
    	 dialog.setTitle("Please Wait for the connection");
         dialog.show();
         dialog.setCancelable(false);
    }
    

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_join_menu, menu);
        return true;
    }

	public void onLocationChanged(Location location) {
		// TODO Auto-generated method stub
		
	}

	public void onProviderDisabled(String provider) {
		// TODO Auto-generated method stub
		
	}

	public void onProviderEnabled(String provider) {
		// TODO Auto-generated method stub
		
	}

	public void onStatusChanged(String provider, int status, Bundle extras) {
		// TODO Auto-generated method stub
		
	}
}